﻿package com.game.view.layer
{
	import AssetClass.SelectRoleInter;
	
	import com.dusk.net.ServerConnect;
	import com.dusk.tool.SubWindow;
	import com.dusk.util.FilterUtil;
	import com.dusk.util.RandomUtil;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.constant.ConfigConst;
	import com.game.manager.GameSceneManager;
	import com.game.manager.NotificationManager;
	import com.game.manager.SoundManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class GameMenu extends LayerBase
	{
		
		public function GameMenu()
		{
			super();
			this.tabChildren = false;
			this.tabEnabled = false;
		}
		
		public var singleGame:TextField;
		public var doubleGame:TextField;
		public var aboutUs:TextField;
		public var cancelSelect:TextField;
		public var continueGame:TextField;
		public var newGame:TextField;
		public var quitGame:TextField;
		public var checkUpdate:TextField;
		
		override protected function beforeAdded():void
		{
			var menu:Sprite = ResUtil.getLayerView("GameMenu") as Sprite;
			TextUtil.redrawTxt(menu);
			singleGame = menu.getChildByName("singleGame") as TextField;
			doubleGame = menu.getChildByName("doubleGame") as TextField;
			cancelSelect = menu.getChildByName("cancelSelect") as TextField;
			
			newGame = menu.getChildByName("newGame") as TextField;
			continueGame = menu.getChildByName("continueGame") as TextField;
			checkUpdate = menu.getChildByName("checkUpdate") as TextField;
			aboutUs = menu.getChildByName("aboutUs") as TextField;
			quitGame = menu.getChildByName("quitGame") as TextField;
			addChild(menu);
		}
		
		public function showSelectNum():void
		{
			if (this.gc.isNew)
			{
				this.singleGame.x = 758;
				this.doubleGame.x = 758;
				this.cancelSelect.x = 758;
				this.newGame.x = 1000;
				this.continueGame.x = 1000;
				this.checkUpdate.x = 1000;
				this.aboutUs.x = 1000;
				this.quitGame.x = 1000;
			}
		}
		
		public function showAndHide():void
		{
			this.gc.memory.getStorage();
			this.doAfterChangeOut();
			this.removeFromParent();
		}
		
		public function showMenu():void
		{
			this.singleGame.x = 1000;
			this.doubleGame.x = 1000;
			this.cancelSelect.x = 1000;
			/////////////////////////////////
			this.aboutUs.x = 758;
			this.continueGame.x = 758;
			this.checkUpdate.x = 758;
			this.newGame.x = 758;
			this.quitGame.x = 758;
		}
		
		public function doAfterChangeOut():void
		{
			if (this.gc.isNew)
			{
				new SelectRoleInter();
			}
			else
			{
				GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MAP);
			}
		}
		
		protected override function onAdded():void
		{
			this.singleGame.addEventListener(MouseEvent.CLICK, this.selectNum);
			this.singleGame.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.singleGame.addEventListener(MouseEvent.ROLL_OUT, this.react);
			this.doubleGame.addEventListener(MouseEvent.CLICK, this.selectNum);
			this.doubleGame.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.doubleGame.addEventListener(MouseEvent.ROLL_OUT, this.react);
			this.cancelSelect.addEventListener(MouseEvent.CLICK, this.cancelClick);
			this.cancelSelect.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.cancelSelect.addEventListener(MouseEvent.ROLL_OUT, this.react);
			//////////////////////////////////////////////////////////////////////
			this.newGame.addEventListener(MouseEvent.CLICK, this.newGameClick);
			this.newGame.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.newGame.addEventListener(MouseEvent.ROLL_OUT, this.react);
			this.continueGame.addEventListener(MouseEvent.CLICK, this.continueClick);
			this.continueGame.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.continueGame.addEventListener(MouseEvent.ROLL_OUT, this.react);
			this.checkUpdate.addEventListener(MouseEvent.CLICK, this.updateClick);
			this.checkUpdate.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.checkUpdate.addEventListener(MouseEvent.ROLL_OUT, this.react);
			this.aboutUs.addEventListener(MouseEvent.CLICK, this.aboutClick);
			this.aboutUs.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.aboutUs.addEventListener(MouseEvent.ROLL_OUT, this.react);
			this.quitGame.addEventListener(MouseEvent.CLICK, this.quitClick);
			this.quitGame.addEventListener(MouseEvent.ROLL_OVER, this.react);
			this.quitGame.addEventListener(MouseEvent.ROLL_OUT, this.react);
			SoundManager.playBgm("begin" + RandomUtil.randInt(1,2).toString());
			if(ServerConnect.getNewVersionId() > ConfigConst.GAME_VERSION_ID)
			{
				NotificationManager.alert("有新版本：" + ServerConnect.getNewVersionName());
			}
		}
		
		override protected function onRemoved():void
		{
			this.singleGame.removeEventListener(MouseEvent.CLICK, this.selectNum);
			this.singleGame.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.singleGame.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			this.doubleGame.removeEventListener(MouseEvent.CLICK, this.selectNum);
			this.doubleGame.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.doubleGame.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			this.cancelSelect.removeEventListener(MouseEvent.CLICK, this.cancelClick);
			this.cancelSelect.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.cancelSelect.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			//////////////////////////////////////////////////////////////////////
			this.newGame.removeEventListener(MouseEvent.CLICK, this.newGameClick);
			this.newGame.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.newGame.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			this.continueGame.removeEventListener(MouseEvent.CLICK, this.continueClick);
			this.continueGame.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.continueGame.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			this.checkUpdate.removeEventListener(MouseEvent.CLICK, this.updateClick);
			this.checkUpdate.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.checkUpdate.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			this.aboutUs.removeEventListener(MouseEvent.CLICK, this.aboutClick);
			this.aboutUs.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.aboutUs.removeEventListener(MouseEvent.ROLL_OUT, this.react);
			this.quitGame.removeEventListener(MouseEvent.CLICK, this.quitClick);
			this.quitGame.removeEventListener(MouseEvent.ROLL_OVER, this.react);
			this.quitGame.removeEventListener(MouseEvent.ROLL_OUT, this.react);
		}
		
		private function updateClick(evt:MouseEvent):void
		{
			SoundManager.playSound("Select");
			ServerConnect.refreshData();
			if(ServerConnect.getNewVersionId() < 0)
			{
				NotificationManager.alert("无法获取更新信息",2);
				return;
			}
			if(ServerConnect.getNewVersionId() > ConfigConst.GAME_VERSION_ID)
			{
				NotificationManager.tip("有新版本：" + ServerConnect.getNewVersionName() + "\n发布时间：" + ServerConnect.getNewVersionPublishTime() + "\n是否查看更新详情？",viewNewInfo,null);
			}
			else
			{
				NotificationManager.alert("已经是最新版本 (" + ConfigConst.GAME_VERSION_NAME + ")");
			}
		}
		
		private function aboutClick(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			GameSceneManager.getIns().addScene(new AboutStudio());
		}
		
		private function cancelClick(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			this.showMenu();
		}
		
		private function continueClick(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			this.gc.isNew = false;
			var panel:SelectArchive = new SelectArchive();
			panel.state = "read";
			this.addChild(panel);
		}
		
		private function newGameClick(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			gc.newGameClick();
			gc.isNew = true;
			showSelectNum();
		}
		
		private function selectNum(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			if (param1.currentTarget.name == "singleGame")
			{
				gc.playNum = 1;
			}
			else
			{
				gc.playNum = 2;
				NotificationManager.alert("双人模式暂未开放");
				return;
			}
			this.showMenu();
			this.doAfterChangeOut();
			this.removeFromParent();
		}
		
		private function quitClick(param1:MouseEvent):void
		{
			SoundManager.playSound("Select");
			stage.nativeWindow.close();
		}
		
		private function viewNewInfo(evt:Event):void
		{
			var win:SubWindow = new SubWindow();
			var text:TextField = TextUtil.creatTextField(ServerConnect.getNewVersionDescription(),TextUtil.UNIVERSAL_FONT,18,"left",0xFFFFFF);
			text.wordWrap = true;
			text.x = text.y = 0;
			text.width = 400;
			text.height = 300;
			win.title = "更新内容";
			win.stage.addChild(text);
			win.width = 420;
			win.height = 320;
			win.alwaysInFront = true;
			win.relocateWindow();
			win.activate();
		}
		
		private function react(evt:MouseEvent):void
		{
			var tf:TextFormat;
			switch (evt.type)
			{
				case MouseEvent.CLICK:
					break;
				case MouseEvent.ROLL_OVER:
					tf = evt.currentTarget.defaultTextFormat;
					tf.color = 0xFFFF00;
					evt.currentTarget.setTextFormat(tf);
					evt.currentTarget.defaultTextFormat = tf;
					FilterUtil.setGlow(evt.currentTarget,0xFFFF00,0.75,5,5,2,3);
					FilterUtil.clearCache();
					break;
				case MouseEvent.ROLL_OUT:
					tf = evt.currentTarget.defaultTextFormat;
					tf.color = 0xFFFFFF;
					evt.currentTarget.setTextFormat(tf);
					evt.currentTarget.defaultTextFormat = tf;
					FilterUtil.deleteAllFilters(evt.currentTarget);
					break;
			}
		}
	}
}
